package cn.smbms.tools;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
@Aspect
public class userAOP {
    private Logger logger= LogManager.getLogger(userAOP.class);

    @Pointcut("execution(public * cn.smbms.service..*.*(..))")
    public void user(){}

    @Before("user()")
    public void before(JoinPoint joinPoint) throws Throwable {
        //方法名
        String name=joinPoint.getSignature().getName();
        //参数
        Object[] objects=joinPoint.getArgs();
        logger.info("方法名："+name+"参数："+ Arrays.toString(objects));
        System.out.println("方法名："+name+"参数："+ Arrays.toString(objects));
        System.out.println("类名："+joinPoint.getTarget()+"类类型："+joinPoint.getTarget().getClass());
    }

    @After("user()")
    public void after(JoinPoint joinPoint){
        //方法名
        String name=joinPoint.getSignature().getName();
        //参数
        Object[] objects=joinPoint.getArgs();
        logger.info("方法名："+name+"参数："+ Arrays.toString(objects));
        System.out.println("类名："+joinPoint.getTarget()+"类类型："+joinPoint.getTarget().getClass());
    }

    @Around("user()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法名
        String name=joinPoint.getSignature().getName();
        //参数
        Object[] objects=joinPoint.getArgs();
        logger.info("方法名："+name+"参数："+ Arrays.toString(objects));
        try{
            System.out.println("类名："+joinPoint.getTarget().getClass().getName()+"类类型："+joinPoint.getTarget().getClass());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            logger.info("返回值："+joinPoint.proceed());
            return  joinPoint.proceed();
        }
    }

     @AfterReturning(value = "user()",returning = "result")
     public void aftereturning(Object result){
         System.out.println("后置返回值："+result);
     }

     @AfterThrowing(value = "user()",throwing = "e")
     public void afterthrowing(Exception e){
         logger.info("异常："+e.getMessage());
     }

}
